Matplotlib makes easy things easy and hard things possible.
matplotlib.pyplot은 그래프를 그릴 때 사용하는 명령어를 모아놓은 것으로, MATLAB의 명령어들과 유사한 기능을 한다.import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x = np.linspace(-10,10)
y = x**2
plt.plot(y, "g*--");
define x, y # input
matplotlib.pyplot.plot(x,y) # create Line2D object
matplotlib.pyplot.show() # renders the object
np.array이므로 다른 유형은 변환하여 사용한다.matplotlib.pyplot은 line plot 이외에도 scatter plot, bar chart, contour map, histogram, pie chart, scatter plot 과 같은 그림 도구를 제공한다.%matplotlib inline은 결과물을 해당 code cell의 아래에 출력%matplotlib notebook은 matplotlib.pyplot.show()과 같은 option을 제공한다.fig = plt.figure()
ax = plt.axes()
plt.plot으로 그림을 그릴 경우 figure와 axes는 암묵적으로 생성이 된다.matplotlib.pyplot.gcf()나 matplotlib.pyplot.gca()으로 활성화 시킬 수 있다.
matplotlib.pyplot.figure(num=None, figsize=None, dpi=None,
facecolor=None, edgecolor=None, frameon=True,
clear=False, **kwargs)
| keywards | default | description |
|---|---|---|
| num | None | activate the referred figure |
| figsize | 6.4x4.8, figure.figsize | figure size in in inches (width, height) |
| dpi | 100, figure.dpi | resolution in dots per inch |
| facecolor | ‘C0’, figure.facecolor | color of the drawing background |
| edgecolor | None, figure.edgecolor | color of edge around the drawing background |
| frameon | True | draw figure frame or not |
| clear | True | if figure is already exists, then it is cleared |
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure
plt.axes(arg=None, **kwargs)
plt.subplot(111, **kwargs)
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.axes.html
matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
| keywards | default | description |
|---|---|---|
| alpha | 1 | opacity, [0,1] |
| linestyle, ls | '-' | {'-', '--', '-.', ':', '', ...} |
| color or c | line color | |
| linewidth or lw | 1 | positive float |
| marker | None | marker style |
| label | None | label for legend |
fmt = "[color][marker][line]"
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.plot.html
line, = plt.plot(x[::5])
| character | color | character | description | character | description | character | description | |
|---|---|---|---|---|---|---|---|---|
| ‘b’ | blue | '-' | solid line style | '<' | triangle_left marker | 'h' | hexagon1 marker | |
| ‘g’ | green | '--' | dashed line style | '>' | triangle_right marker | 'H' | hexagon2 marker | |
| ‘r’ | red | '-.' | dash-dot line style | '1' | tri_down marker | '+' | plus marker | |
| ‘c’ | cyan | ':' | dotted line style | '2' | tri_up marker | 'x' | x marker | |
| ‘m’ | magenta | '.' | point marker | '3' | tri_left marker | 'D' | diamond marker | |
| ‘y’ | yellow | ',' | pixel marker | '4' | tri_right marker | 'd' | thin_diamond marker | |
| ‘k’ | black | 'o' | circle marker | 's' | square marker | ' | ' | vline marker |
| ‘w’ | white | 'v' | triangle_down marker | 'p' | pentagon marker | '_' | hline marker | |
| '^' | triangle_up marker | '*' | star marker |
line = plt.plot(x[::5], marker='o', color='dimgray', markerfacecolor='lightgray', markeredgecolor="gray")
xmin, xmax = plt.xlim( (xmin, xmax) )
locs, labels = plt.xticks(locs, labels)
plt.xscale(scale) #scale = 'linear', 'log', 'logit', 'symlog'
plt.plot(x[::5], marker='o', color='dimgray', markerfacecolor='lightgray', markeredgecolor="gray")
plt.xlim([-1,10])
plt.ylim([-12,11])
plt.xticks(range(11), list(range(10))+[''])
plt.grid(axis='y')
plt.title(f'plot of {len(x[::5]):d} observations', fontsize=16);
plt.figure(figsize=(7,5))
x = np.arange(11)
plt.plot(x**2, label=r"quadratic function, $f(x)=x^2$")
plt.plot(x**3, label=r"cubic function, $f(x)=x^3$")
plt.xlim(0,10)
plt.ylim(0,300)
plt.xlabel(r"$x$", fontsize=14)
plt.xticks([1,3,8,9], fontsize=14)
plt.ylabel(r"$f(x)$", fontsize=14)
plt.legend(fontsize=14)
plt.grid(axis = "x", which='major')
plt.suptitle("LINE PLOTS", fontsize=16)
plt.tight_layout()
plt.subplot이나 fig.add_subplot으로 어렵지 않게 구성할 수 있다.fig.add_axes를 사용할 수 있다.fig = plt.figure(figsize=(5,4))
plt.subplot(221)
plt.subplot(223)
plt.subplot(122);
import matplotlib.font_manager as fm
path = r"C:\Users\K5\AppData\Local\Microsoft\Windows\Fonts\NanumGothicBold.ttf"
NanumGothic = fm.FontProperties(fname = path, size=12)
plt.title('한글', fontproperties=NanumGothic, fontsize=16);
plt.style.use(['default'])
from matplotlib import rcParams
import matplotlib.font_manager as fm
#NanumGothic = fm.FontProperties(fname=path)
#rcParams['font.family'] = NanumGothic.get_name()
rcParams['font.family'] = 'NanumGothic'
# label에 minus sign이 제대로 표시되기 않는 경우에 삽입
rcParams["axes.unicode_minus"] = False
plt.title('한글', fontsize=16)
plt.style.use('default')
define x, y # input
figure, axes = plt.subplots() # create class
axes.plot(x,y) # create Line2D object
plt.show() # renders the object
fig, axes = plt.subplots(nrows=1, ncols=1, sharex=False, sharey=False, **fig_kw)
fig.add_subplot(nrows, ncols, index, **kwargs)
fig.add_axes([left, bottom, width, height], **kwargs)
fig = plt.figure(figsize=(8,6))
fig.add_subplot(221)
plt.plot(x**2)
fig.add_subplot(223)
plt.plot(x**3)
fig.add_subplot(122)
fig.add_axes([.2,.2,.5,.5]);
ax.plot()은 객체 ax에 method로 작용한다.line, = ax.plot(sample_size, var_of_sample_mean, "b")
line.set_label(r"variance of $\overline{X}$")
plt.xlim과 ax.set_xlim와 같은 형태로 비슷하게 사용된다.xmin, xmax = np.min(sample), np.max(sample)
x_range = xmax - xmin
x_margin = 0.1*x_range
ax.set_xlim(xmin-x_margin, xmax+x_margin)
ax.set_xticks(sample[::3])
ax.set_xticklabels(sample[::3])
ax.text(x, y, s)
transform=ax.transAxes로 axes의 상대적인 위치를 사용할 수 있다.pyton
ax.annotation()
pytnon
ax.annotate(text, xy, xytext, *args, **kwargs)
data = pd.read_csv("https://github.com/k5yi/econ2005/blob/master/datasets/teen_birth.txt?raw=True", sep="\t")
random_sampling = np.random.RandomState(1).randint(1, len(data), size = 8)
data = data.loc[random_sampling]
x = data.PovPct.to_numpy()
y = data.Brth15to17.to_numpy()
state = data.Location.to_numpy()
slope, intercept = np.polyfit(x, y, deg=1)
f = lambda x: intercept + slope*x
x_unit = (np.max(x) - np.min(x))/50
y_unit = (np.max(y) - np.min(y))/50
xlim=(np.min(x) - 10*x_unit, np.max(x) + 10*x_unit)
ylim=(np.min(y) - 10*y_unit, np.max(y) + 10*y_unit)
fig, ax = plt.subplots(figsize=(8,6))
ax.plot(xlim, [f(x) for x in xlim], c='gray', lw=2, label="fitted line")
ax.scatter(x, y, ec='dimgray', c="lightgray", s=20, label="sample")
for i in range(len(y)):
# data point to the fitted line
label="residual/error" if i == 1 else ""
ax.plot([x[i],x[i]], [y[i], f(x[i])], "r-", lw = 1.2, label=label)
if y[i] >= f(x[i]):
ax.annotate(state[i], xy=(x[i] - x_unit, y[i] + y_unit), fontsize=12)
else: ax.annotate(state[i], xy=(x[i] - x_unit,y[i] - 3*y_unit), fontsize=12)
ax.set(xlim=xlim, ylim=ylim)
ax.axhline(y = np.mean(y), c="lightgray", lw=1, label=r"mean of $y$")
ax.set_xlabel("Poverty Rate", fontsize=14)
ax.set_ylabel("Birth Rate of Age 15-17", fontsize=14)
ax.legend(fontsize=14, scatterpoints=3)
fig.suptitle("Deviations", fontsize=16)
fig.tight_layout()
import seaborn as sns
plt.style.use('ggplot')
plt.plot(x);
sns.reset_orig()
fig, axes = plt.subplots(1, 2, figsize=(8,4))
with plt.xkcd():
axes[0].plot(x)
axes[1].plot(y)
fig.tight_layout()
2-1. 평균과 분산은 confidence interval을 이용하여 표시한다. 2-2. x축을 0과 1로 정규화한 후 빈도를 bin의 수가 12개인 histogram으로 그려본다.
sample_sizes = np.logspace(.75, 3, 24).astype(int)
print(sample_sizes)
def mean_variance(n, p=.3, resampling=20):
means = np.mean(np.random.RandomState(42).choice([0,1], (resampling, n), p=(1-p,p)), axis=-1)
mean = np.mean(means)
std = np.std(means)
return mean, std
mean = np.array([])
std = np.array([])
for i in sample_sizes:
m, s = mean_variance(i, p=.3)
mean = np.append(mean, m)
std = np.append(std, s)
fig, ax = plt.subplots(figsize=(8,6))
ax.plot(sample_sizes, mean+1.96*std)
ax.plot(sample_sizes, np.maximum(0,mean-1.96*std))
ax.plot(sample_sizes, mean);
fig, ax = plt.subplots(figsize=(8,6))
ax.plot(sample_sizes, mean+1.96*std, color="gray")
ax.plot(sample_sizes, np.maximum(0,mean-1.96*std), color="gray")
ax.fill_between(sample_sizes, np.maximum(0,mean-1.96*std), mean+1.96*std, color='lightgray')
ax.plot(sample_sizes, mean, color = "dimgray")
ax.set_xscale('log')
fig, ax = plt.subplots(figsize=(8,6))
ax.plot(sample_sizes, mean, color="dimgray", label='Sample Mean')
ax.plot(sample_sizes, mean+1.96*std, color="gray")
ax.plot(sample_sizes, np.maximum(0,mean-1.96*std), color="gray")
ax.fill_between(sample_sizes, np.maximum(0,mean-1.96*std), mean+1.96*std,
color='lightgray', label=r'95% confidence interval')
ax.set_xscale('log')
ax.set(ylim=[-0.1, .8], yticks = np.arange(0,.71,.1))
ax.legend(fontsize=14)
ax.grid(which="both")
fig.suptitle('Figure 1. Sample Mean and Variance of Sample Mean', fontsize=16)
fig.tight_layout();
from scipy.stats import binom
n, p = 100, 0.3
mean, var, skew, kurt = binom.stats(n, p, moments='mvsk')
fig, ax = plt.subplots(figsize=(8,6))
x = np.arange(binom.ppf(0.00001, n, p),
binom.ppf(0.99999, n, p))
ax.plot(x/n, binom.pmf(x, n, p), color='dimgray', label='binom pmf')
ax.set_xlim([-0.1, .8])
y_min = 0
y_max = binom.pmf(mean, n, p)
y_range = y_max - y_min
ylim = [y_min - 0.1*y_range, y_max + 0.1*y_range]
ax.set_ylim(*ylim)
p_hat = r'$\hat{p}$'
ax.axhline(0, color='lightgray')
#ax.axvline(x=mean/n, ymin=0, ymax=binom.pmf(mean, n, p), color='lightgray')
ax.set_title(f'Binomial Distibution: n = {n:d}, {p_hat:s} = {mean/n:.2f}', fontsize=16)
plt.tight_layout()
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
fig, ax = plt.subplots(figsize=(8,6))
def binom_density(n, p=0.3):
mean = binom.stats(n, p, moments='m')
x = np.arange(binom.ppf(0.00001, n, p),
binom.ppf(0.99999, n, p))
ax.clear()
ax.plot(x/n, binom.pmf(x, n, p), color='dimgray', ms=8, label='binom pmf')
ax.set_xlim([-0.1, .8])
ax.set_ylim([0, 1.1*np.max(binom.pmf(x, n, p))])
p_hat = r'$\hat{p}$'
ax.axhline(0, color='lightgray')
ax.set_title(f'Binomial Distibution: n = {n:d}, {p_hat:s} = {mean/n:.2f}', fontsize=16)
return line
animator = FuncAnimation(fig, binom_density, frames=range(2, 100, 10), interval=500)
#HTML(animator.to_html5_video())
HTML(animator.to_jshtml())
import seaborn as sns
x = np.random.RandomState(42).randn(3,3)
corr = np.corrcoef(x)
sns.set(font_scale=1.4)
sns.heatmap(corr, annot=True);
import plotly.express as px
df = px.data.iris()
px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species')